package com.jeeplus.modules.web.wx;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.TimeUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.web.entity.YjMahjongHallRoom;
import com.jeeplus.modules.web.entity.YjUserRoomQrcode;
import com.jeeplus.modules.web.entity.YjMahjongHall;
import com.jeeplus.modules.web.service.YjMahjongHallRoomService;
import com.jeeplus.modules.web.service.YjUserRoomQrcodeService;
import com.jeeplus.modules.web.service.YjMahjongHallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author liangbing
 * @version 1.0
 * @date 2021/4/1 8:46
 * @description 微信用户-棋牌室房间二维关系controller
 */
@Api(value = "YjUserRoomQrcodeController", description = "微信用户-棋牌室房间二维关系")
@Controller
@RequestMapping(value = "wx/userRoomQrcode")
public class YjUserRoomQrcodeController extends BaseController {

    @Autowired
    private YjUserRoomQrcodeService yjUserRoomQrcodeService;

    @Autowired
    private YjMahjongHallService yjMahjongHallService;

    @Autowired
    private YjMahjongHallRoomService yjMahjongHallRoomService;

    /**
     * 新建用户-棋牌室房间二维码关系
     * @param yjUserRoomQrcode
     * @return
     */
    @ApiOperation(notes = "create", httpMethod = "POST", value = "新建用户-棋牌室房间二维码关系")
    @ResponseBody
    @PostMapping(value = "bind")
    public AjaxJson bind(@RequestBody @Validated YjUserRoomQrcode yjUserRoomQrcode) {
        AjaxJson j = new AjaxJson();

        // 检查二维码是否有效
        YjMahjongHallRoom yjMahjongHallRoom = yjMahjongHallRoomService.getRoomByQrcode(yjUserRoomQrcode.getRoomQrcode());
        if (yjMahjongHallRoom == null) {
            j.setSuccess(false);
            j.setMsg("二维码不存在或者未绑定房间！");
            return j;
        }

        // 检查当前棋牌室是否已经开通了呼叫功能
        String hallId = yjMahjongHallRoom.getMahjongHall().getId();
        YjMahjongHall yjMahjongHall = yjMahjongHallService.get(hallId);
        if (!new Integer(1).equals(yjMahjongHall.getIsOpenCall())) {
            j.setSuccess(false);
            j.setMsg("当前棋牌室未开通呼叫功能");
            return j;
        }

        // 新建用户-房间二维码关系
        yjUserRoomQrcode.setLastActivateDate(new Date());
        yjUserRoomQrcodeService.insertOrUpdate(yjUserRoomQrcode);

        j.setSuccess(true);
        j.getBody().put("data", yjMahjongHallRoom);
        return j;
    }

    /**
     * 检查用户是否关联了有效的房间二维码
     * @param openid
     * @return
     */
    @ApiOperation(notes = "isCurrentUserBindValidRoomQrcode", httpMethod = "GET", value = "检查房间二维码是否有效")
    @ApiImplicitParams({@ApiImplicitParam(name = "openid", value = "用户openid", required = true, paramType = "query", dataType = "string")})
    @ResponseBody
    @GetMapping(value = "isCurrentUserBindValidRoomQrcode")
    public AjaxJson isCurrentUserBindValidRoomQrcode(String openid) {
        AjaxJson j = new AjaxJson();

        // 检查用户是否关联了有效的房间二维码
        YjUserRoomQrcode record = yjUserRoomQrcodeService.getByOpenid(openid);
        if (record == null) {
            j.setSuccess(false);
            j.setMsg("二维码不存在或者已过期！");
            return j;
        }

        // 检查当前绑定的棋牌室是否开通了呼叫功能
        String hallRoomId = record.getMahjongHallRoom().getId();
        YjMahjongHallRoom yjMahjongHallRoom = yjMahjongHallRoomService.get(hallRoomId);
        String hallId = yjMahjongHallRoom.getMahjongHall().getId();
        YjMahjongHall yjMahjongHall = yjMahjongHallService.get(hallId);
        if (!new Integer(1).equals(yjMahjongHall.getIsOpenCall())) {
            j.setSuccess(false);
            j.setMsg("当前棋牌室未开通呼叫功能");
            return j;
        }

        j.setSuccess(true);
        return j;
    }
}
